
package Model;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;


public class PDFCreater {

    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
//    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
//            Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
//    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
//            Font.BOLD);

    public static void createPDF(String file, ThongTinNguoiNhan infopayment, GioHang cart) throws FileNotFoundException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        document.newPage();
        addMetaData(document);

        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Hoa Don Mua Hang", catFont));

        addEmptyLine(preface, 1);

        document.add(preface);
        addContent(document, infopayment, cart);
        addTable(document, cart);
        document.close();



    }

    public static void addContent(Document document, ThongTinNguoiNhan payment, GioHang cart) throws DocumentException {
        Paragraph p = new Paragraph();
        p.add(new Paragraph("Ten Khach hang: " + payment.getThongTinUser().getHoTen(), subFont));
        p.add(new Paragraph("Dia chi gia hang: " + payment.getThongTinUser().getDiaChi(), subFont));
        p.add(new Paragraph("Dien Thoai: " + payment.getThongTinUser().getDienThoai(), subFont));
        p.add(new Paragraph("CMND: " + payment.getCMND(), subFont));
        p.add(new Paragraph("Phuong thuc Thanh Toan: chuyen khoan", subFont));
        p.add(new Paragraph("Thanh Tien: " + cart.getTotal(), subFont));
        addEmptyLine(p, 3);
        document.add(p);
    }

    private static void addMetaData(Document document) {
        document.addTitle("Mobile Shop");
        document.addSubject("Hoa Don");
        document.addKeywords("Java, PDF, Mobile Shop");
        document.addAuthor("Nguyen Xuan Vinh Hung");
        document.addCreator("Nguyen Xuan Vinh Hung");
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private static void addTable(Document document, GioHang cart) throws DocumentException {
        PdfPTable table = new PdfPTable(4);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Ma Hang"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Gia"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("So Luong"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        
        c1 = new PdfPCell(new Phrase("Thanh Tien"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        ArrayList<Mobile> list= cart.getGioHang();
        for(Mobile item : list){
            table.addCell(item.getTenSP());
            table.addCell(item.getGiaBan() + "");
            table.addCell(item.getSoLuong() + "");
            table.addCell(item.getTotalCurrencyFormat());
        }
                
 
        
        document.add(table);

    }

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
     //   InfoPayment info = new InfoPayment("haha", "haha", "haha", "haha", "haha", "haha", "haha", "haha");
        GioHang cart = new GioHang();
        
     //   createPDF("D:\\hoadone.pdf", info, cart);
    }
}
